﻿Imports System.Data.OleDb, System.Drawing

Public Class WebForm2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            If Request.Params("ID") = "" Then Response.Redirect("WebForm1.aspx")
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("travelConnectionString").ConnectionString)
            Dim SqlString As String = "Select * From Comments Where Fk_Id = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            oleDbConn.Open()
            Dim dataReader = cmd.ExecuteReader()
            GridView2.DataSource = dataReader
            GridView2.DataBind()

            Dim hotelsViewedSql As String = "Insert Into HotelsViewed(Visitor,Hotel_fkId) Values (@f1,@f2)"
            Dim cmd2 As OleDbCommand = New OleDbCommand(hotelsViewedSql, oleDbConn)
            cmd2.CommandType = CommandType.Text
            cmd2.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd2.Parameters.AddWithValue("@f2", Request.Params("ID"))
            cmd2.ExecuteNonQuery()
        End If


    End Sub

    Protected Sub bt_AddComment_Click(sender As Object, e As EventArgs) Handles bt_AddComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("travelConnectionString").ConnectionString)
        Dim SqlString As String = "Insert Into Comments(Author, Title, Comment , Fk_Id) Values (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        'Dim myGUID, myGUID2 As New System.Guid
        'myGUID = Guid.NewGuid()
        'myGUID2 = Guid.NewGuid()
        'Dim newFileName = myGUID.ToString() + ".jpg"
        'Dim newFileName2 = myGUID2.ToString() + ".jpg"

        'If F_Picture.HasFile Then
        'F_Picture.SaveAs(Request.MapPath("pictures") & "/" & newFileName)
        'Using OriginalImage = Image.FromFile(Request.MapPath("pictures") & "/" & newFileName)
        'Using ResizedImage As New Bitmap(OriginalImage, 500, 500)
        'ResizedImage.Save(Request.MapPath("pictures") & "/" & newFileName2, Drawing.Imaging.ImageFormat.Jpeg)

        'End Using
        'End Using
        'End If
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_CommentTitle.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        'Response.Redirect("confirmation2.aspx")
        'fill GridView2
        Dim fillComments As String = "Select * From Comments Where Fk_Id = @f1"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.CommandType = CommandType.Text
        fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dataReader = fillCommentsCmd.ExecuteReader()
        GridView2.DataSource = dataReader
        GridView2.DataBind()

        tb_CommentTitle.Text = ""
        tb_Comment.Text = ""


    End Sub

    Protected Sub GridView2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView2.SelectedIndexChanged

    End Sub

    Protected Sub bt_TagHotel_Click(sender As Object, e As EventArgs) Handles bt_TagHotel.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("travelConnectionString").ConnectionString)
        Dim SqlString As String = "Insert Into HotelTagLink(Hotel_FkId, Tag_FkId) Values (@f1,@f2)"
        oleDbConn.Open()
        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                'populate the HotelTagLink table
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next
        'add new tags
        If TextBox1.Text = "" Then Return
        Dim newTags = TextBox1.Text.Split(",")
        For Each newTag In newTags
            'add new tag to table
            Dim newTagSql As String = "Insert Into Tags(Tag) Values (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()
            'find id of the new tag
            Dim getLastIdCmd As OleDbCommand = New OleDbCommand("Select Top 1 ID From Tags Order By Id Desc", oleDbConn)
            Dim dr = getLastIdCmd.ExecuteReader()
            dr.Read()
            Dim lastId = dr.GetValue(0)
            'associate id with hotel id
            Dim newcmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            newcmd.CommandType = CommandType.Text
            newcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            newcmd.Parameters.AddWithValue("@f2", lastId)
            newcmd.ExecuteNonQuery()

        Next

    End Sub

    Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class